//对localstorge实现二次封装，提高安全性
const namespace = 'information'

export default {
    setItem(key, val) {
        //先获取最外层的别名key值
        let storage = this.getStorage()
        storage[key] = val
        window.localStorage.setItem(namespace, JSON.stringify(storage))
    },
    getStorage() {
        try {  
            // 尝试解析 localStorage 中的数据  
            return JSON.parse(window.localStorage.getItem(namespace) || '{}');  
        } catch (e) {  
            // 如果解析失败，返回空对象  
            console.error(e);  
            return {};  
        } 
    },
    getItem(key) {
        return this.getStorage()[key]
    },
    clearItem(key) {
        let storage = this.getStorage()
        delete storage[key]
        window.localStorage.setItem(namespace, JSON.stringify(storage))
    },
    //清除local
    clearAll() {
        window.localStorage.removeItem(namespace)
    }
}

